

import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;

/**
 * SetHelper contains methods for performing certain actions on sets, since
 * the built-in contains() wasn't appropriate.
 *
 */
public class SetHelper {
	
	/**
	 * Performs a search in a set, using Predicate::equals() for comparison
	 * @param set
	 * @param p
	 * @return True if p was found in set, false otherwise
	 */
	public static boolean contains(HashSet<Predicate> set, Predicate p) {
		Iterator<Predicate> iter = set.iterator();
		
		while (iter.hasNext()) {
			Predicate foo = iter.next();
			
			if (foo.equals(p))
				return true;
		}
		
		return false;
	}
	
	/**
	 * Performs a search in a set, using Predicate::equals2() for comparison
	 * @param set
	 * @param p
	 * @return True if p was found in set, false otherwise
	 */
	public static boolean contains2(HashSet<Predicate> set, Predicate p) {
		Iterator<Predicate> iter = set.iterator();
		
		while (iter.hasNext()) {
			Predicate foo = iter.next();
			
			if (foo.equals2(p))
				return true;
		}
		
		return false;
	}
	/**
	 * Removes a predicate from a HashSet
	 * @param set
	 * @param p
	 * @return
	 */
	public static HashSet<Predicate> remove(HashSet<Predicate> set, Predicate p){

		HashSet<Predicate> newSet = new HashSet<Predicate>(); 
		
		Iterator<Predicate> iter = set.iterator();
		
		while (iter.hasNext()) {
			Predicate pre = iter.next();
			
			if (! (pre).equals2(p) || pre instanceof Ingredient){
				newSet.add(pre);
			}
		}

		return newSet;
	}
	
	/**
	 * Returns a new vector where all first-occuring elements in v2 has been removed from v1 (if they existed)
	 * @param v1
	 * @param v2
	 * @return
	 */
	public static Vector<Resource> removeAll(Vector<Resource> v1, Vector<Resource> v2) {
		Vector<Resource> ret = new Vector<Resource>(v1);
		
		for (Resource r : v2) {
			ret.remove(r);
		}
		
		return ret;
		
	}
}
